<?php
/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\adminapi\controller\v1\diy;


use app\common\controller\AdminController;
use think\App;

class AddonDiy extends AdminController
{
    /**
     * @var \app\common\model\diy\AddonDiy
     */
    private $model;
    private array $where;


    public function __construct(App $app)
    {
        parent::__construct($app);
        $key = input('key', '');
        $type = input('type', '');
        $plaid = input('plaid', $this->request->header('plaid'));
        $addon = $this->request->header('addon_name');
        $this->where = ['type' => $type, 'plaid' => $plaid, 'key' => $key, 'addon' => $addon];
        $this->model = new \app\common\model\diy\AddonDiy();
    }

    //保存
    public function save()
    {
        $diyData = input('diyData', []);
        $diy = $this->model->where($this->where)->findOrEmpty();
        $data = array(
            'value' => $diyData
        );
        $diy->save(array_merge($data, $this->where));
        return $this->success('保存成功', array_merge($data, $this->where));
    }

    //获取
    public function get()
    {
        $diy = $this->model->where($this->where)->find();
        if (!$diy) {
            $value = [
                'rule'   => [],
                'config' => [],
            ];
        } else {
            $value = $diy['value'];
        }
        return $this->success('ok', $value);
    }
}
